/**
 * @author:albert
 */

package com.bkbw.live.battle.business.skill.ability;

import java.util.HashMap;

import org.apache.commons.math3.geometry.euclidean.threed.Vector3D;

import scala.reflect.generic.Trees.This;

import com.badlogic.gdx.math.Vector3;
import com.bkbw.live.common.dataproto.EnumConfigPb.BulletCollisionMode;
import com.bkbw.live.common.dataproto.EnumConfigPb.BulletMoveMode;
import com.bkbw.live.battle.business.BehaviorBase;
import com.bkbw.live.battle.business.BulletObject;
import com.bkbw.live.battle.business.UnitObject;
import com.bkbw.live.battle.business.behaviors.BezierMove3;
import com.bkbw.live.battle.business.behaviors.BulletMoveBehavior;
import com.bkbw.live.battle.business.skill.AbilityBase;
import com.bkbw.live.battle.business.skill.effect.DamageEffect;

public class ShootAbility extends AbilityBase {
	
	float missileSpeed;
	BulletMoveMode moveMode;
	BulletCollisionMode collisionMode;
	
	public ShootAbility(String abilityId, HashMap<String, Object> param) {
		super(abilityId, param);
		missileSpeed = (float)param.get("missileSpeed");
		moveMode = (BulletMoveMode)param.get("moveMode");
		collisionMode = (BulletCollisionMode)param.get("collisionMode");
	}

	@Override
	public void spellAnim()
	{
		this.unit.getMap().attackNotify(this.unit.getId(), this.unit.getTargetEnemy().getId());
		System.out.println("id " + this.unit.getId() + "shoot bullet anim");
	}
	
	@Override
	public void trigger()
	{
		BulletObject bulletObject = this.unit.getMap().createBullet();
		bulletObject.setPosition(this.unit.getPosition());
		HashMap<String, Object> param = new HashMap<String, Object>();
		param.put("srcUnit", this.unit);
		param.put("dstUnit", this.unit.getTargetEnemy());
		param.put("srcPos", this.unit.getPosition());
		param.put("dstPos", this.unit.getTargetEnemy().getPosition());
		param.put("missileSpeed", missileSpeed);
		param.put("moveMode", moveMode);
		param.put("collisionMode", collisionMode);
		bulletObject.bindBehavior("BulletMoveBehavior", param);
		
		if (moveMode == BulletMoveMode.Curve) {
			BezierMove3 curveMove3 = new BezierMove3(new Vector3((float)this.unit.getPosition().getX(), 0, (float)this.unit.getPosition().getZ()),
					new Vector3((float)this.unit.getTargetEnemy().getPosition().getX(), 0, (float)this.unit.getTargetEnemy().getPosition().getZ()), 
					BezierMove3.BezierHeight, missileSpeed);
			Vector3 pos = new Vector3();
			Vector3 derivate = new Vector3();
			curveMove3.getFirst(pos, derivate);
			this.unit.getMap().bulletCreateNotify(bulletObject.getId(), new Vector3D(pos.x, pos.y, pos.z),
					new Vector3D(derivate.x, derivate.y, derivate.z),
					missileSpeed);
			BehaviorBase behaviorBase = bulletObject.getBehavior("BulletMoveBehavior");
			if (behaviorBase != null) {
				BulletMoveBehavior bulletMoveBehavior = (BulletMoveBehavior) behaviorBase;
				bulletMoveBehavior.setCurveMove3(curveMove3);
			}
			
			//curveMove = new BezierMove(new Vector2((float)this.srcPos.getX(), (float)this.srcPos.getZ()),  
			//		new Vector2((float)this.dstPos.getX(), (float)this.dstPos.getZ()), BezierMove.Angle60, this.missileSpeed);
		}
		else {
			this.unit.getMap().bulletCreateNotify(bulletObject.getId(), this.unit.getPosition(),this.unit.getPosition(), missileSpeed);
		}
		
		//System.out.println("src " + this.unit.getPosition().toString());
		//System.out.println("dst " + this.unit.getTargetEnemy().getPosition().toString());
		
		
		System.out.println("shoot bullet trigger");
	}
}
